home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / rdflib / Collection.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.0 KB  |  212 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from rdflib import RDF, BNode, Literal
  5. from rdflib.Graph import Graph
  6.  
  7. class Collection(object):
  8.     """
  9.     See 3.3.5 Emulating container types: http://docs.python.org/ref/sequence-types.html#l2h-232
  10.     
  11.     >>> listName = BNode()
  12.     >>> g = Graph('IOMemory')
  13.     >>> listItem1 = BNode()
  14.     >>> listItem2 = BNode()
  15.     >>> g.add((listName,RDF.first,Literal(1)))
  16.     >>> g.add((listName,RDF.rest,listItem1))
  17.     >>> g.add((listItem1,RDF.first,Literal(2)))
  18.     >>> g.add((listItem1,RDF.rest,listItem2))
  19.     >>> g.add((listItem2,RDF.rest,RDF.nil))
  20.     >>> g.add((listItem2,RDF.first,Literal(3)))
  21.     >>> c=Collection(g,listName)
  22.     >>> print list(c)
  23.     [rdflib.Literal('1', language=None, datatype=rdflib.URIRef('http://www.w3.org/2001/XMLSchema#int')), rdflib.Literal('2', language=None, datatype=rdflib.URIRef('http://www.w3.org/2001/XMLSchema#int')), rdflib.Literal('3', language=None, datatype=rdflib.URIRef('http://www.w3.org/2001/XMLSchema#int'))]
  24.     >>> 1 in c
  25.     True
  26.     >>> len(c)
  27.     3
  28.     >>> c._get_container(1) == listItem1
  29.     True
  30.     >>> c.index(Literal(2)) == 1
  31.     True
  32.     """
  33.     
  34.     def __init__(self, graph, uri, seq = []):
  35.         self.graph = graph
  36.         if not uri:
  37.             pass
  38.         self.uri = BNode()
  39.         for item in seq:
  40.             self.append(item)
  41.         
  42.  
  43.     
  44.     def _get_container(self, index):
  45.         '''Gets the first, rest holding node at index.'''
  46.         if not isinstance(index, int):
  47.             raise AssertionError
  48.         graph = self.graph
  49.         container = self.uri
  50.         i = 0
  51.         while i < index:
  52.             i += 1
  53.             container = graph.value(container, RDF.rest)
  54.             if container is None:
  55.                 break
  56.                 continue
  57.             isinstance(index, int)
  58.         return container
  59.  
  60.     
  61.     def __len__(self):
  62.         '''length of items in collection.'''
  63.         count = 0
  64.         for item in self.graph.items(self.uri):
  65.             count += 1
  66.         
  67.         return count
  68.  
  69.     
  70.     def index(self, item):
  71.         '''
  72.         Returns the 0-based numerical index of the item in the list          
  73.         '''
  74.         listName = self.uri
  75.         index = 0
  76.         while True:
  77.             if (listName, RDF.first, item) in self.graph:
  78.                 return index
  79.             newLink = list(self.graph.objects(listName, RDF.rest))
  80.             index += 1
  81.             if newLink == [
  82.                 RDF.nil]:
  83.                 raise ValueError('%s is not in %s' % (item, self.uri))
  84.             newLink == [
  85.                 RDF.nil]
  86.             if not newLink:
  87.                 raise Exception('Malformed RDF Collection: %s' % self.uri)
  88.             newLink
  89.             if not len(newLink) == 1:
  90.                 raise AssertionError, 'Malformed RDF Collection: %s' % self.uri
  91.             listName = newLink[0]
  92.             continue
  93.             len(newLink) == 1
  94.  
  95.     
  96.     def __getitem__(self, key):
  97.         '''TODO'''
  98.         c = self._get_container(key)
  99.         if c:
  100.             v = self.graph.value(c, RDF.first)
  101.             if v:
  102.                 return v
  103.             raise KeyError, key
  104.         c
  105.         raise IndexError, key
  106.  
  107.     
  108.     def __setitem__(self, key, value):
  109.         '''TODO'''
  110.         c = self._get_container(key)
  111.         if c:
  112.             self.graph.add((c, RDF.first, value))
  113.         else:
  114.             raise IndexError, key
  115.         return c
  116.  
  117.     
  118.     def __delitem__(self, key):
  119.         '''...'''
  120.         self[key]
  121.         graph = self.graph
  122.         current = self._get_container(key)
  123.         if not current:
  124.             raise AssertionError
  125.  
  126.     
  127.     def __iter__(self):
  128.         '''Iterator over items in Collections'''
  129.         return self.graph.items(self.uri)
  130.  
  131.     
  132.     def append(self, item):
  133.         container = self.uri
  134.         graph = self.graph
  135.         while True:
  136.             first = graph.value(container, RDF.first)
  137.             if first is None:
  138.                 graph.add((container, RDF.first, item))
  139.                 return None
  140.             rest = graph.value(container, RDF.rest)
  141.             if rest:
  142.                 container = rest
  143.                 continue
  144.             first is None
  145.             node = BNode()
  146.             graph.add((container, RDF.rest, node))
  147.             container = node
  148.  
  149.     
  150.     def clear(self):
  151.         container = self.uri
  152.         graph = self.graph
  153.         while container:
  154.             rest = graph.value(container, RDF.rest)
  155.             graph.remove((container, RDF.first, None))
  156.             graph.remove((container, RDF.rest, None))
  157.             container = rest
  158.  
  159.  
  160.  
  161. def test():
  162.     import doctest
  163.     doctest.testmod()
  164.  
  165. if __name__ == '__main__':
  166.     test()
  167.     g = Graph()
  168.     c = Collection(g, BNode())
  169.     if not len(c) == 0:
  170.         raise AssertionError
  171.     c = Collection(g, BNode(), [
  172.         Literal('1'),
  173.         Literal('2'),
  174.         Literal('3'),
  175.         Literal('4')])
  176.     if not len(c) == 4:
  177.         raise AssertionError
  178.     if not c[1] == Literal('2'):
  179.         raise AssertionError, c[1]
  180.     del c[1]
  181.     if not list(c) == [
  182.         Literal('1'),
  183.         Literal('3'),
  184.         Literal('4')]:
  185.         raise AssertionError, list(c)
  186.     
  187.     try:
  188.         del c[500]
  189.     except IndexError:
  190.         list(c) == [
  191.             Literal('1'),
  192.             Literal('3'),
  193.             Literal('4')]
  194.         i = list(c) == [
  195.             Literal('1'),
  196.             Literal('3'),
  197.             Literal('4')]
  198.         c[1] == Literal('2')
  199.     except:
  200.         len(c) == 4
  201.  
  202.     c.append(Literal('5'))
  203.     print list(c)
  204.     for i in c:
  205.         print i
  206.     
  207.     del c[3]
  208.     c.clear()
  209.     if not len(c) == 0:
  210.         raise AssertionError
  211.  
  212.